package com.greydawn.CompositePattern;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhongwr
 * @Create: 2021/12/19 17:01
 * @Version 1.0
 * @Description:
 */
public class Composite extends Component {

    private List<Component> leafs = new ArrayList<>();

    public Composite(String name) {
        this.name = name;
    }

    @Override
    public void add(Component component) {
        leafs.add(component);
    }

    @Override
    public void remove(Component component) {
        leafs.remove(component);
    }

    @Override
    public void display() {
        System.out.println("--" + this.name);
        for (int i = 0; i < leafs.size(); i++) {
            leafs.get(i).display();
        }
    }
}
